package com.itheima.controller;

import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.resources.MessageConstant;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/checkItem",name = "检查项管理模块")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @RequestMapping(value = "/addCheckItem.do",name = "新增功能")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        //try...catch
        try {
            //调用生产者添加方法
            checkItemService.addCheckItem(checkItem);
            //添加成功:返回一个Result对象
            return new Result(true,"添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            //返回Result对象
            return new Result(false,"添加失败!");
        }


    }

    @RequestMapping(value = "/findPageCheckItem.do",name="分页查询功能")
    public Result findPageCheckItem(@RequestBody QueryPageBean queryPageBean){
        try {
            //调用生产者的查询方法
            Map map=checkItemService.findPageCheckItem(queryPageBean);
            return new Result(true,"查询成功!",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败!");
        }
    }

    @RequestMapping(value = "/deleteCheckItemById.do",name="删除功能")
    public Result deleteCheckItemById(Integer id){

        try {
            //调用生产者的删除方法
            checkItemService.deleteCheckItemById(id);
            return new Result(true,"删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败!");
        }

    }

    @RequestMapping(value = "/updateCheckItem.do",name="修改功能")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        try {
            //调用生产者的修改方法
            checkItemService.updateCheckItem(checkItem);
            return new Result(true,"修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败!");
        }

    }

    @RequestMapping(value = "/findAllCheckItem.do",name="查询所有功能")
    public Result findAllCheckItem(){
        try {
            //调用生产者的修改方法
            List<CheckItem> checkItemList=checkItemService.findAllCheckItem();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
}
